<template>
  <div class="loginPage">
    <div class="login_box">
      <div class="login_title">应用市场管理平台</div>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="login_content"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <el-button slot="prepend" icon="iconfont  icon-yonghu"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password">
            <el-button slot="prepend" icon="iconfont  icon-mima"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="verifycode">
          <el-input v-model="loginForm.verifycode" placeholder="请输入验证码" style="width:180px">
            <el-button slot="prepend" icon="iconfont  icon-yanzhengma"></el-button>
          </el-input>
        <canvas id="canvas" @click="getVerifyCode"></canvas>
        </el-form-item>
        <div class="login_btn">
          <el-button type="primary" @click="submitForm()" :loading="loading">登录</el-button>
        </div>
        <div class="login_tips">Tips : 用户名和密码随便填。</div>
      </el-form>
    </div>
  </div>
</template>

<script>
import validCodeObj from '../../utils/validCode'
import { commonGetValidCode } from '../../service/common'
export default {
  data() {
    return {
      loginForm: {
        username: "hzc",
        password: "123456",
        verifycode: ""
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        verifycode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            min: 4,
            max: 4,
            message: "长度为4个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getVerifyCode() //获取验证码
  },
  methods: {
    async getVerifyCode() {
      const res = await commonGetValidCode()
      if (res.code == '200') {
       sessionStorage.setItem('token',res.data.token)
       let vCode = res.data.verifyCode
       this.loginForm.verifycode = vCode  
       let vArr = []
       for (const key in vCode) {
         vArr.push(vCode[key])
       }
       validCodeObj.draw(vArr)
      }else {
        console.log('验证码接口错误')
      }
    },
    submitForm() {
      this.$refs.login.validate(async (valid) => {
        if (valid) {
          // this.$router.push('/home')
            this.loading = true
            console.log(this.loginForm,'登录参数')
           const res = await this.$store.dispatch('Login', this.loginForm)
              this.loading = false
              console.log(res, '登录res')
              if (res.code == "200") {
               sessionStorage.setItem('token',res.data.token)
               this.$message.success("登录成功");
               this.$router.push({path: '/'})
              }else {
                this.$message.error("账号/密码错误");
              }
        } else {
          this.$message.error("请输入账号,密码,验证码");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.loginPage {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url(../../assets/imgs/login-bg.jpg);
  background-size: 100%;

  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 350px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);

    .login_title {
      width: 100%;
      line-height: 50px;
      text-align: center;
      font-size: 20px;
      color: #fff;
      border-bottom: 1px solid #ddd;
    }

    .login_content {
      padding: 30px 30px;

      #canvas {
        display: inline-block;
        width: 100px;
        height: 40px;
        margin-left: 10px;
        border: 1px solid #eee;
      }

      .login_btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
      }

      .login_tips {
        font-size: 12px;
        line-height: 30px;
        color: #fff;
      }
    }
  }
}
</style>